import appsettings from "@/appsettings";


/**
 * 常用工具
 */
export class CommonHelper {

    /**
     * 用于生产GUIID字符串
     * @returns 
     */
    private static S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };

    /**
     * 获取GuID
     * @returns 
     */
    static getGUID() {
        return (this.S4() + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + this.S4() + this.S4());
    }

    /**
     * 获取没有-符号的GUID
     * @returns 
     */
    static getGUIDAsNot = () => {
        return this.getGUID().replace("-", "");
    }


    /**
      * 下载excel表格
      * @param url 请求地址
      * @param datas 请求参数
      */
    static downloadExcel(url: string, datas: any) {
        let _url = `${appsettings.ApiBaseURL}${url}`;
        let form = document.createElement('form');
        form.id = "ExportExcel";
        form.name = "ExportExcel";
        form.action = _url;
        form.method = 'post';
        Object.keys(datas).forEach(key => {
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = key;
            input.value = datas[key];
            form.appendChild(input);
        });
        document.body.appendChild(form)
        form.submit();
        form.remove();
    }

    /**
     * 笛卡尔交集算法
     * @param array 
     * @returns 
     */
    static Descartes = (array: any) => {
        if (array.length < 2) return array[0] || [];
        // @ts-ignore
        return [].reduce.call(array, function (col, set) {
            let res: any = [];
            // @ts-ignore
            col.forEach(function (c) {
                // @ts-ignore
                set.forEach(function (s) {
                    // @ts-ignore
                    let t = [].concat(Array.isArray(c) ? c : [c]);
                    // @ts-ignore
                    t.push(s);
                    res.push(t);
                })
            });
            return res;
        });
    }

}